class Analyse {
  constructor() {

  }
  // 日照分析
  sunlight() {
    var viewer = new Cesium.Viewer('cesiumContainer', {
      selectionIndicator: false,
      useBrowserRecommendedResolution: false,
      animation: false, //是否显示动画控件
      baseLayerPicker: false, //是否显示图层选择控件
      geocoder: false, //是否显示地名查找控件
      timeline: false, //是否显示时间线控件
      sceneModePicker: true, //是否显示投影方式控件
      navigationHelpButton: false, //是否显示帮助信息控件
      infoBox: false, //是否显示点击要素之后显示的信息
      imageryProvider: Cesium.createOpenStreetMapImageryProvider({
        url: 'https://a.tile.openstreetmap.org/'
      }),
      fullscreenButton: true,
    })

    //取消双击事件
    viewer.cesiumWidget.screenSpaceEventHandler.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_DOUBLE_CLICK);

    //开启深度检测
    viewer.scene.globe.depthTestAgainstTerrain = true;

    var stopTime = null;
    // 开始旋转
    function stratPlay() {
      if (viewer.clock.shouldAnimate = !0, stopTime) {
        viewer.clock.currentTime = stopTime;
      }
      else {
        var e = "2021-12-26",
          t = new Date(e),
          i = "10",
          a = "18",
          r = new Date(new Date(t).setHours(Number(i))),
          o = new Date(new Date(t).setHours(Number(a)));
        viewer.scene.globe.enableLighting = !0,
          viewer.shadows = !0,
          viewer.clock.startTime = Cesium.JulianDate.fromDate(r),
          viewer.clock.currentTime = Cesium.JulianDate.fromDate(r),
          viewer.clock.stopTime = Cesium.JulianDate.fromDate(o),
          viewer.clock.clockRange = Cesium.ClockRange.LOOP_STOP,
          viewer.clock.clockStep = Cesium.ClockStep.SYSTEM_CLOCK_MULTIPLIER,
          viewer.clock.multiplier = 1600
      }
    }

    // 停止旋转
    function stopPlay() {
      stopTime = viewer.clock.currentTime
      viewer.clock.shouldAnimate = !1
    }

    // 点击按钮
    function setvisible(value) {
      switch (value) {
        case 'play':
          stratPlay();
          break;
        case 'stop':
          stopPlay();
          break;
      }
    }

  }
  // 淹没分析
  submerge() {
    Cesium.tileSize = 256
    Cesium.Ion.defaultAccessToken = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiI5MWVkMjllNy03ZjY4LTQ3YmQtOTgxOC05NGQ5YTU0ZjM5ZGEiLCJpZCI6MzU5Nywic2NvcGVzIjpbImFzciIsImdjIl0sImlhdCI6MTUzODE5MTUyM30.dtS2F3-q2fGoA93N7cFl-LCikK-Rjk7v01WWA-RqCxg'
    var cesiumTerrainProvider = Cesium.createWorldTerrain()

    var viewer = new Cesium.Viewer('cesiumContainer', {
      selectionIndicator: false,
      terrainProvider: cesiumTerrainProvider, // 加载地形
      useBrowserRecommendedResolution: false,
      animation: false, //是否显示动画控件
      baseLayerPicker: false, //是否显示图层选择控件
      geocoder: false, //是否显示地名查找控件
      timeline: false, //是否显示时间线控件
      sceneModePicker: true, //是否显示投影方式控件
      navigationHelpButton: false, //是否显示帮助信息控件
      infoBox: false, //是否显示点击要素之后显示的信息
      imageryProvider: false,
      fullscreenButton: true,
    })

    //天地图影像图层
    var tiandituImageLayer = new Cesium.ImageryLayer(
      new Cesium.WebMapTileServiceImageryProvider({
        url: 'http://{s}.tianditu.gov.cn/img_w/wmts?service=wmts&request=GetTile&version=1.0.0' + '&LAYER=img&tileMatrixSet=w&TileMatrix={TileMatrix}&TileRow={TileRow}&TileCol={TileCol}' + '&style=default&format=tiles&tk=440b96d736fad95da7934bc130a2258a',
        layer: 'tdtImg_c',
        style: 'default',
        format: 'tiles',
        tileMatrixSetID: 'c',
        subdomains: ['t0', 't1', 't2', 't3', 't4', 't5', 't6', 't7'],
        tilingScheme: new Cesium.WebMercatorTilingScheme(),
        tileMatrixLabels: ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19'],
        maximumLevel: 20,
      })
    )
    viewer.imageryLayers.add(tiandituImageLayer)

    // 设置视图
    viewer.camera.setView({
      destination: Cesium.Cartesian3.fromDegrees(120.3056859, 30.37667624, 1500),
      orientation: {
        heading: Cesium.Math.toRadians(340.0876405649615),
        pitch: Cesium.Math.toRadians(-9.965309016806488),
        roll: Cesium.Math.toRadians(359.94808234298637),
      },
    })
    var position = Cesium.Cartesian3.fromDegrees(120.3056859, 30.37667624, 500.0);


    function measureAreaSpace(viewer, maxH, interval, speed) {
      let waterEntity = viewer.entities.add({
        polygon: {
          hierarchy: Cesium.Cartesian3.fromDegreesArray([ //hierarchy分层（位置，孔）
            120.2756859, 30.42667624,
            120.2734762, 30.43048772,
            120.2691198, 30.43816092,
            120.2688253, 30.43887895,
            120.2721572, 30.44058701,
            120.2753432, 30.44222015,
            120.2781081, 30.44363731,
            120.2831174, 30.4462046,
            120.2842303, 30.44613582,
            120.284472, 30.44590172,
            120.2875494, 30.44292078,
            120.2904976, 30.44006473,
            120.293151, 30.43749407,
            120.2970764, 30.43281576,
            120.2970761, 30.43263726,
            120.2956152, 30.43206517,
            120.2834947, 30.42860944,
            120.2756859, 30.42667624

          ]),
          material: Cesium.Color.POWDERBLUE.withAlpha(0),
          perPositionHeight: false,
          extrudedHeight: 0.0,
          disableDepthTestDistance: Number.POSITIVE_INFINITY,
          heightReference: Cesium.HeightReference.CLAMP_TO_GROUND //是否贴地
        }
      });
      // 两秒后开始进入淹没分析
      setTimeout(function () {
        if (waterEntity) {
          waterEntity.polygon.material = Cesium.Color.POWDERBLUE.withAlpha(0.5);
          var h = 0;
          waterEntity.polygon.extrudedHeight = 0; //需要提前设置 不然会全部出现
          var st = setInterval(function () {
            h = h + speed;
            if (h >= maxH) {
              h = maxH; //给个最大值
              alert("达到最高值");
              clearTimeout(st);
            }
            waterEntity.polygon.extrudedHeight.setValue(h);
          }, interval);
        }

        /* 多边形*/
        var PolygonPrimitive = (function () {
          function _(positions) {
            this.options = {
              name: '多边形',
              polygon: {
                hierarchy: [],
                material: Cesium.Color.POWDERBLUE.withAlpha(0.5)
              }
            };

            this.hierarchy = positions;
            this._init();
          }
          _.prototype._init = function () {
            var _self = this;
            var _update = function () {
              return _self.hierarchy;
            };
            //实时更新polygon.hierarchy
            this.options.polygon.hierarchy = new Cesium.CallbackProperty(_update, false);
            waterEntity = viewer.entities.add(this.options);
          };

          return _;
        })();

      }, interval);
    }


    function runwater() {
      var maxHeight = document.getElementById('maxHeight').value;
      var minHeight = document.getElementById('minHeight').value;
      var speed = document.getElementById('speed').value;
      //运行
      measureAreaSpace(viewer, maxHeight, 1000, 1); //maxH 设置为2000;最大淹没海拔 每10毫秒前进1海拔;
    }

    function removewater() {
      viewer.entities.removeAll();

    }
    viewer.clock.onTick.addEventListener(function () {
      if (viewer.camera.pitch > 0) {
        viewer.scene.screenSpaceCameraController.enableTilt = false;
      }
    });
    var mousePosition, startMousePosition;
    var handler = new Cesium.ScreenSpaceEventHandler(viewer.canvas);
    handler.setInputAction(function (movement) {
      mousePosition = startMousePosition = Cesium.Cartesian3.clone(movement.position);
      handler.setInputAction(function (movement) {
        mousePosition = movement.endPosition;
        var y = mousePosition.y - startMousePosition.y;
        if (y > 0) {
          viewer.scene.screenSpaceCameraController.enableTilt = true;
        }
      }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
    }, Cesium.ScreenSpaceEventType.MIDDLE_DOWN);

    viewer.scene.globe.depthTestAgainstTerrain = true;
  }
}
